package com.example.lightsabercontroller

import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import com.example.lightsabercontroller.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var mBtController: BluetoothController
    private lateinit var bluetoothChangeReceiver: BluetoothChangeReceiver

    inner class BluetoothChangeReceiver: BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            when (intent?.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) ?: 0) {
                BluetoothAdapter.STATE_OFF -> {
                    showToast("STATE_OFF")
//                    mBtController.turnOnBluetooth(, 0)
                }
                BluetoothAdapter.STATE_ON -> showToast("STATE_ON")
                BluetoothAdapter.STATE_TURNING_ON -> showToast("STATE_TURNING_ON")
                BluetoothAdapter.STATE_TURNING_OFF -> showToast("STATE_TURNING_OFF")
                else -> showToast("Unknown State")
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        mBtController = BluetoothController()

        if (!mBtController.isSupportBluetooth()) {
            showToast("该机器不支持蓝牙！")
            finish()
        }
        else {
            showToast("设备支持蓝牙！")
            mBtController.turnOnBluetooth(this, 0)
        }

        val intentFilter = IntentFilter()
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED)
        bluetoothChangeReceiver = BluetoothChangeReceiver()
        registerReceiver(bluetoothChangeReceiver, intentFilter)

        binding.connectButton.setOnClickListener {
            val intent = Intent(this, ModeSelectionActivity::class.java)
            startActivity(intent)
        }

    }

    private fun isSupportBluetooth() {
        val ret = mBtController.isSupportBluetooth()
        showToast("是否支持蓝牙？$ret")
    }

    private fun isBluetoothEnable() {
        val ret = mBtController.getBluetoothStatus()
        showToast("蓝牙是否开启？$ret")
    }

    private fun requestTurnOnBluetooth() {
        /**
         * 申请打开蓝牙
         */
        mBtController.turnOnBluetooth(this, 0)
    }

    private fun turnOffBluetooth() {
        mBtController.turnOffBluetooth()
    }

    private fun sendMessageToEsp() {
        for (device in mBtController.getBondedDevices()) {
            Log.d("蓝牙", device.name)
            if (device.name.startsWith("LightSaber")) {
                if (mBtController.connectToLightSaber(device.address)) {
                    mBtController.btSendData("test")
                }
            }
        }
    }

    /**
     * 申请打开蓝牙的回调函数
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                showToast("成功打开蓝牙")
            }
            else {
                showToast("打开蓝牙失败")
            }
        }
    }

    private fun showToast(text: String) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.test1 -> Toast.makeText(this, "You clicked test1", Toast.LENGTH_SHORT).show()
            R.id.test2 -> Toast.makeText(this, "You clicked test2", Toast.LENGTH_SHORT).show()
        }
        return true
    }
}